#include<stdio.h>
int hexToDec(char *ch)
{
int flag=0;
int hex = 0;
int n;
while (*ch != '\0')
{
if (*ch<='9'&&*ch>='0')
{
flag = 1;
n = *ch - '0';
hex = hex * 16 + n;
++ch;
}
else if (*ch<='f'&&*ch>='a')
{
flag = 1;
n = *ch - 'a' + 10;
hex = hex * 16 + n;
++ch;
}
else if (*ch<='F'&&*ch>='A')
{
flag = 1;
n = *ch - 'A' + 10;
hex = hex * 16 + n;
++ch;
}
else
{
if (flag)
break;
else
++ch;
}
}
return hex;
}
int main()
{
char hex[10];
int decimal = 0;
printf("Input a Hex numbers: ");
gets(hex);
decimal=hexToDec(hex);
printf("The decimal value is : %d\n",decimal);
return 0;
}
十六进制数转十进制
最新推荐文章于 2022-03-01 15:32:26 发布